﻿Imports System.IO.StreamWriter
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ''gogogadget xQ! chromableedstudios ( a+ ) gmail.com
        GoGoGadget()
    End Sub
    ''main program logic - does everything
    Sub GoGoGadget()

        ''1 check for valid rapiconfig and template.xml
        If CheckForQFiles() = True Then

            ''2 read mdn box and check for valid input
            '' write mdn into delete-and-set.xml
            GetAndChangeMdn()

            ''3 execute cmd line rapi and redirect standard output to textbox
            ResultTextB.Text = GetProcessText(My.Application.Info.DirectoryPath & "\RapiConfig.exe", "/p delete-n-set.xml", My.Application.Info.DirectoryPath)

            ''4 ?

            ''6 profit!
        ElseIf CheckForQFiles() = False Then
            ''throw no qfiles error
            MessageBox.Show("couldn't find rapiconfig or \Template\delete-n-set.xml")
        End If
    End Sub

    Function CheckForQFiles()

        If System.IO.File.Exists(My.Application.Info.DirectoryPath & "\RapiConfig.exe")  Then


            If System.IO.File.Exists(My.Application.Info.DirectoryPath & "\Template\delete-n-set.xml") Then
                ''Check for the qfiles if found return true and proceed with program
                ''MessageBox.Show("found it")
                Return True
            Else
                ''if the .xml files are not found return false and do not proceed
                Return False
            End If

        Else
            ''if the rapi files are not found return false and do not proceed
            Return False
        End If

    End Function

    Sub GetAndChangeMdn()
        If MaskedTextBox1.MaskCompleted = True Then

            NewMdn = MaskedTextBox1.Text
            ''call sub to change mdn in template to user entered mdn
            changeMdn()
            ''display number to user (to check for typos)
            MessageBox.Show("Wap and MMS setup for: " + MaskedTextBox1.Text)

        Else
            MessageBox.Show("Please enter a MDN to setup.")
        End If

    End Sub

    ''declare MDN to be read from user input
    Public NewMdn As String
    Sub changeMdn()
        ''read the template xml file
        Dim reader As New System.IO.StreamReader(My.Application.Info.DirectoryPath & "\Template\delete-n-set.xml")
        Dim sLine As String
        sLine = reader.ReadToEnd()
        reader.Close()

        ''find the mdn and write new mdn to xml file
        Dim writer As New System.IO.StreamWriter(My.Application.Info.DirectoryPath & "\delete-n-set.xml")
        writer.Write(sLine.Replace("5055505050", NewMdn))
        writer.Close()
    End Sub

    ''borrowed liberally from: http://www.developerfusion.com/code/4662/execute-a-process-and-fetch-its-output/
    ''this Function will run the cmd line program and then redirect the standard output as a return string

    Shared Function GetProcessText(ByVal process As String, ByVal param As String, ByVal workingDir As String) As String
        ''System.Diagnostics.Process.Start(My.Application.Info.DirectoryPath & "\RapiConfig.exe")
        Dim p As Process = New Process
        ' this is the name of the process we want to execute
        p.StartInfo.FileName = process
        If Not (workingDir = "") Then
            p.StartInfo.WorkingDirectory = workingDir
        End If
        p.StartInfo.Arguments = param
        ' need to set this to false to redirect output
        p.StartInfo.UseShellExecute = False
        p.StartInfo.RedirectStandardOutput = True
        ' start the process
        p.Start()
        ' read all the output
        ' here we could just read line by line and display it
        ' in an output window
        Dim output As String = p.StandardOutput.ReadToEnd
        ' wait for the process to terminate
        p.WaitForExit()
        Return output
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If System.IO.File.Exists(My.Application.Info.DirectoryPath & "\Template\delete-n-set.xml") Then
            ''Check for the qfiles if found return true and proceed with program
            ''MessageBox.Show("found it")
            GetAndChangeMdn()
        Else
            ''if the .xml files are not found return false and do not proceed
            MessageBox.Show("\Template\delete-n-set.xml is not found, ensure it is there and has 5055505050 as the mdn")
        End If



    End Sub


  
    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
        ''about
        MessageBox.Show("this program is provided as is no warranty gpl v3 bugs, questions, job offers ha! chromableedstudios ( a+ ) gmail ( dot ) com")
    End Sub
End Class
